<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2022/1/17
 * Time: 14:09
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

declare(strict_types=1);

namespace app\api\middleware;

use app\common\lib\Show;
use app\common\lib\Status;
use app\common\model\MallUser;

class Auth
{
    public function handle($request, \Closure $next)
    {
        /**
         *
         */

        if (!preg_match('/login/', $request->pathinfo())) {
            $server = $request->server();
            if (!boolval(env('APP_DEBUG', false))) {
                if (strpos($server['HTTP_USER_AGENT'], 'MicroMessenger') === false) {
                    return Show::json_show(Status::mini_error, "非法请求");
                }
            }
            $param = $request->request();
            if (empty($param['openid'])) {
                return Show::json_show(Status::mini_error, "用户识别不存在");
            }else{
               $user= (new MallUser())->getBaseByWhereOne([['openid','=',$param['openid']]]);
               if($user['status'] ==0 && $user){
                   return Show::json_show(881, "账号被封禁");
               }
            }
            $request->openid = $param['openid'];
        }

        return $next($request);
    }
}